package cn.stylefeng.guns.sys.modular.system.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.listener.HyczExcelListener;
import cn.stylefeng.guns.sys.core.util.JsonResult;
import cn.stylefeng.guns.sys.modular.system.entity.BtyHycz;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyHyczMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyDbdkParam;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyHyczParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyHyczResult;
import cn.stylefeng.guns.sys.modular.system.service.BtyHyczService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;


/**
 * 控制器
 *
 * @author liuy
 * @Date 2022-06-02 16:57:05
 */
@Controller
@RequestMapping("/btyHycz")
public class BtyHyczController extends BaseController {

    private String PREFIX = "/modular/system/btyHycz";

    @Autowired
    private BtyHyczService btyHyczService;
    @Resource
    private BtyHyczMapper  hyczMapper;

    /**
     * 跳转到主页面
     *
     * @author liuy
     * @Date 2022-06-02
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/btyHycz.html";
    }

    /**
     * 新增页面
     *
     * @author liuy
     * @Date 2022-06-02
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/btyHycz_add.html";
    }

    /**
     * 编辑页面
     *
     * @author liuy
     * @Date 2022-06-02
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/btyHycz_edit.html";
    }

    /**
     * 新增接口
     *
     * @author liuy
     * @Date 2022-06-02
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(BtyHyczParam btyHyczParam) {
        this.btyHyczService.add(btyHyczParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author liuy
     * @Date 2022-06-02
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(BtyHyczParam btyHyczParam) {
        this.btyHyczService.update(btyHyczParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author liuy
     * @Date 2022-06-02
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(BtyHyczParam btyHyczParam) {
        this.btyHyczService.delete(btyHyczParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author liuy
     * @Date 2022-06-02
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(BtyHyczParam btyHyczParam) {
        BtyHycz detail = this.btyHyczService.getById(btyHyczParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author liuy
     * @Date 2022-06-02
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BtyHyczParam btyHyczParam) {
        return this.btyHyczService.findPageBySpec(btyHyczParam);
    }

    /**
     * 导入excel
     */
    @RequestMapping("/import")
    @ResponseBody
    public Object addUsers(@RequestParam("file") MultipartFile file, BtyDbdkParam param) {
        JsonResult jsonResult = new JsonResult();
        if(ToolUtil.isEmpty(param.getZdrq())){
            jsonResult.setState(0);
            jsonResult.setErrmsg("请先选择制单日期！");
            return jsonResult;
        }
        //导入之前删掉之前的数据
        hyczMapper.delete(new QueryWrapper<>());
        String zdrq=param.getZdrq();
        try {
            HyczExcelListener basicExcelListener = new HyczExcelListener(btyHyczService);
            EasyExcel.read(file.getInputStream(), BtyHycz.class, basicExcelListener).sheet(0).doRead();
//            //导入成功之后生成会员卡凭证
           List<BtyHyczResult>list=hyczMapper.hyczList();
            for (BtyHyczResult result : list) {
                String yaoma=result.getYaoma();
                String bh=result.getBh();
                String kh=result.getKh();
                BigDecimal zje=result.getZje();
                String dm=result.getDm();
                //会员现金
                BigDecimal hyxj =result.getXjcz()==null?BigDecimal.ZERO:result.getXjcz();
                //会员支付宝
                BigDecimal hyzfb = result.getZfbcz()==null?BigDecimal.ZERO:result.getZfbcz();
                //会员微信
                BigDecimal hywx = result.getWxcz()==null?BigDecimal.ZERO:result.getWxcz();
                //会员买单
                BigDecimal hymd = result.getDpmd()==null?BigDecimal.ZERO:result.getDpmd();
                //会员建行
                BigDecimal hyjh = result.getJhsk()==null?BigDecimal.ZERO:result.getJhsk();
                //抖音收款
                BigDecimal dysk = result.getDysk()==null?BigDecimal.ZERO:result.getDysk();
                //商场收款
                BigDecimal scsk = result.getScsk()==null?BigDecimal.ZERO:result.getScsk();
                //云闪付
                BigDecimal ysf = result.getYsf()==null?BigDecimal.ZERO:result.getYsf();

                RestTemplate restTemplate = new RestTemplate();
                HttpHeaders headers = new HttpHeaders();
                MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
                headers.setContentType(type);
                headers.add("trantype", "code"); // 档案翻译方式，枚举值为：编码请录入 code， 名称请录入 name， 主键请录入 pk
                headers.add("system", "001"); // 系统编码
                headers.add("usercode", "15253172037"); // 用户
                headers.add("password", "514576f58a89767b46c1b44767a08f77");
                headers.add("Accept", MediaType.APPLICATION_JSON.toString());
                StringBuilder sb = new StringBuilder();
                sb.append("{\n" +
                        "    \"voucher\":[\n" +
                        "        {\n" +
                        "            \"details\":[");
                //会员现金凭证
                if (hyxj.compareTo(BigDecimal.ZERO) == 1) {
                    sb.append("                {\n" +
                            "                    \"ass\":[\n" +
                            "                        {\n" +
                            "                            \"checktypecode\":\"2\",\n" +
                            "                            \"checkvaluecode\":'" +yaoma+ "'\n" +
                            "                        },\n" +
                            "                        {\n" +
                            "                            \"checktypecode\":\"7\",\n" +
                            "                            \"checkvaluecode\":'0038'\n" +//人民币编码
                            "                        }\n" +
                            "                    ],\n" +
                            "                    \"creditamount\":\"\",\n" +
                            "                    \"debitamount\":\""+hyxj+"\",\n" +
                            "                    \"explanation\":\"会员卡充值支出\",\n" +
                            "                    \"localcreditamount\":\"\",\n" +
                            "                    \"localdebitamount\":\"\",\n" +
                            "                    \"pk_accsubj\":\"112203\",\n" +
                            "                    \"pk_currtype\":\"CNY\"\n" +
                            "                },");
                }
                //会员微信凭证
                if (hywx.compareTo(BigDecimal.ZERO) == 1) {
                    sb.append("                {\n" +
                            "                    \"ass\":[\n" +
                            "                        {\n" +
                            "                            \"checktypecode\":\"2\",\n" +
                            "                            \"checkvaluecode\":'" +yaoma+ "'\n" +
                            "                        },\n" +
                            "                        {\n" +
                            "                            \"checktypecode\":\"7\",\n" +
                            "                            \"checkvaluecode\":'0172'\n" +//微信收款编码
                            "                        }\n" +
                            "                    ],\n" +
                            "                    \"creditamount\":\"\",\n" +
                            "                    \"debitamount\":\""+hywx+"\",\n" +
                            "                    \"explanation\":\"会员卡充值支出\",\n" +
                            "                    \"localcreditamount\":\"\",\n" +
                            "                    \"localdebitamount\":\"\",\n" +
                            "                    \"pk_accsubj\":\"112206\",\n" +
                            "                    \"pk_currtype\":\"CNY\"\n" +
                            "                },");
                }
                //会员支付宝凭证
                if (hyzfb.compareTo(BigDecimal.ZERO) == 1) {
                    sb.append("                {\n" +
                            "                    \"ass\":[\n" +
                            "                        {\n" +
                            "                            \"checktypecode\":\"2\",\n" +
                            "                            \"checkvaluecode\":'" +yaoma+ "'\n" +
                            "                        },\n" +
                            "                        {\n" +
                            "                            \"checktypecode\":\"7\",\n" +
                            "                            \"checkvaluecode\":'0189'\n" +//支付宝收款编码
                            "                        }\n" +
                            "                    ],\n" +
                            "                    \"creditamount\":\"\",\n" +
                            "                    \"debitamount\":\""+hyzfb+"\",\n" +
                            "                    \"explanation\":\"会员卡充值支出\",\n" +
                            "                    \"localcreditamount\":\"\",\n" +
                            "                    \"localdebitamount\":\"\",\n" +
                            "                    \"pk_accsubj\":\"112207\",\n" +
                            "                    \"pk_currtype\":\"CNY\"\n" +
                            "                },");
                }
                //会员买单凭证
                if (hymd.compareTo(BigDecimal.ZERO) == 1) {
                    sb.append("                {\n" +
                            "                    \"ass\":[\n" +
                            "                        {\n" +
                            "                            \"checktypecode\":\"2\",\n" +
                            "                            \"checkvaluecode\":'" +yaoma+ "'\n" +
                            "                        },\n" +
                            "                        {\n" +
                            "                            \"checktypecode\":\"7\",\n" +
                            "                            \"checkvaluecode\":'0015'\n" +//点评买单编码
                            "                        }\n" +
                            "                    ],\n" +
                            "                    \"creditamount\":\"\",\n" +
                            "                    \"debitamount\":\""+hymd+"\",\n" +
                            "                    \"explanation\":\"会员卡充值支出\",\n" +
                            "                    \"localcreditamount\":\"\",\n" +
                            "                    \"localdebitamount\":\"\",\n" +
                            "                    \"pk_accsubj\":\"112208\",\n" +
                            "                    \"pk_currtype\":\"CNY\"\n" +
                            "                },");
                }


                //会员建行凭证 不是安徽大区的生到建行收款
                if (hyjh.compareTo(BigDecimal.ZERO) == 1 && !bh.equals("305")) {
                    sb.append("                {\n" +
                            "                    \"ass\":[\n" +
                            "                        {\n" +
                            "                            \"checktypecode\":\"2\",\n" +
                            "                            \"checkvaluecode\":'" +yaoma+ "'\n" +
                            "                        },\n" +
                            "                        {\n" +
                            "                            \"checktypecode\":\"7\",\n" +
                            "                            \"checkvaluecode\":'0024'\n" +//建行收款编码
                            "                        }\n" +
                            "                    ],\n" +
                            "                    \"creditamount\":\"\",\n" +
                            "                    \"debitamount\":\""+hyjh+"\",\n" +
                            "                    \"explanation\":\"会员卡充值支出\",\n" +
                            "                    \"localcreditamount\":\"\",\n" +
                            "                    \"localdebitamount\":\"\",\n" +
                            "                    \"pk_accsubj\":\"112209\",\n" +
                            "                    \"pk_currtype\":\"CNY\"\n" +
                            "                },");
                }
                //会员建行凭证 安徽大区的生到商场收款
                if (hyjh.compareTo(BigDecimal.ZERO) == 1 && bh.equals("305")) {
                    sb.append("                {\n" +
                            "                    \"ass\":[\n" +
                            "                        {\n" +
                            "                            \"checktypecode\":\"2\",\n" +
                            "                            \"checkvaluecode\":'" +yaoma+ "'\n" +
                            "                        },\n" +
                            "                        {\n" +
                            "                            \"checktypecode\":\"7\",\n" +
                            "                            \"checkvaluecode\":'0056'\n" +//商场收款编码
                            "                        }\n" +
                            "                    ],\n" +
                            "                    \"creditamount\":\"\",\n" +
                            "                    \"debitamount\":\""+hyjh+"\",\n" +
                            "                    \"explanation\":\"会员卡充值支出\",\n" +
                            "                    \"localcreditamount\":\"\",\n" +
                            "                    \"localdebitamount\":\"\",\n" +
                            "                    \"pk_accsubj\":\"112219\",\n" +
                            "                    \"pk_currtype\":\"CNY\"\n" +
                            "                },");
                }
                //商场收款凭证
                if (scsk.compareTo(BigDecimal.ZERO) == 1) {
                    sb.append("                {\n" +
                            "                    \"ass\":[\n" +
                            "                        {\n" +
                            "                            \"checktypecode\":\"2\",\n" +
                            "                            \"checkvaluecode\":'" +yaoma+ "'\n" +
                            "                        },\n" +
                            "                        {\n" +
                            "                            \"checktypecode\":\"7\",\n" +
                            "                            \"checkvaluecode\":'0056'\n" +//商场收款编码
                            "                        }\n" +
                            "                    ],\n" +
                            "                    \"creditamount\":\"\",\n" +
                            "                    \"debitamount\":\""+scsk+"\",\n" +
                            "                    \"explanation\":\"会员卡充值支出\",\n" +
                            "                    \"localcreditamount\":\"\",\n" +
                            "                    \"localdebitamount\":\"\",\n" +
                            "                    \"pk_accsubj\":\"112219\",\n" +
                            "                    \"pk_currtype\":\"CNY\"\n" +
                            "                },");
                }
                //抖音收款凭证
                if (dysk.compareTo(BigDecimal.ZERO) == 1) {
                    sb.append("                {\n" +
                            "                    \"ass\":[\n" +
                            "                        {\n" +
                            "                            \"checktypecode\":\"2\",\n" +
                            "                            \"checkvaluecode\":'" +yaoma+ "'\n" +
                            "                        },\n" +
                            "                        {\n" +
                            "                            \"checktypecode\":\"7\",\n" +
                            "                            \"checkvaluecode\":'0017'\n" +//商场收款编码
                            "                        }\n" +
                            "                    ],\n" +
                            "                    \"creditamount\":\"\",\n" +
                            "                    \"debitamount\":\""+dysk+"\",\n" +
                            "                    \"explanation\":\"会员卡充值支出\",\n" +
                            "                    \"localcreditamount\":\"\",\n" +
                            "                    \"localdebitamount\":\"\",\n" +
                            "                    \"pk_accsubj\":\"112220\",\n" +
                            "                    \"pk_currtype\":\"CNY\"\n" +
                            "                },");
                }
                //云闪付凭证
                if (ysf.compareTo(BigDecimal.ZERO) == 1) {
                    sb.append("                {\n" +
                            "                    \"ass\":[\n" +
                            "                        {\n" +
                            "                            \"checktypecode\":\"2\",\n" +
                            "                            \"checkvaluecode\":'" +yaoma+ "'\n" +
                            "                        },\n" +
                            "                        {\n" +
                            "                            \"checktypecode\":\"7\",\n" +
                            "                            \"checkvaluecode\":'0394'\n" +//云闪付编码
                            "                        }\n" +
                            "                    ],\n" +
                            "                    \"creditamount\":\"\",\n" +
                            "                    \"debitamount\":\""+ysf+"\",\n" +
                            "                    \"explanation\":\"会员卡充值支出\",\n" +
                            "                    \"localcreditamount\":\"\",\n" +
                            "                    \"localdebitamount\":\"\",\n" +
                            "                    \"pk_accsubj\":\"112219\",\n" +
                            "                    \"pk_currtype\":\"CNY\"\n" +
                            "                },");
                }
                sb.append("{\n" +
                        "                    \"ass\":[\n" +
                        "                        {\n" +
                        "                            \"checktypecode\":\"2\",\n" +
                        "                            \"checkvaluecode\":'" +yaoma+ "'\n" +
                        "                        },\n" +
                        "                        {\n" +
                        "                            \"checktypecode\":\"96\",\n" +
                        "                            \"checkvaluecode\":" + kh + "\n" +
                        "                        }\n" +
                        "                    ],\n" +
                        "                    \"creditamount\":\""+ zje + "\",\n" +
                        "                    \"debitamount\":\"\",\n" +
                        "                    \"explanation\":\"会员卡充值支出\",\n" +
                        "                    \"localcreditamount\":\"\",\n" +
                        "                    \"localdebitamount\":\"\",\n" +
                        "                    \"pk_accsubj\":\"100202\",\n" +
                        "                    \"pk_currtype\":\"CNY\"\n" +
                        "                }");
                sb.append("            ],\n" +
                        "            \"pk_corp\":"+bh+",\n" +
                        "            \"pk_glorgbook\":\""+bh+"-bty01\",\n" +
                        "            \"pk_prepared\":\"15253172037\",\n" +
                        "            \"pk_vouchertype\":\"记账\",\n" +
                        "            \"prepareddate\":"+zdrq+"\n" +
                        "        }\n" +
                        "    ]\n" +
                        "}");
               // System.out.println("sb:"+sb);
                HttpEntity<String> formEntity = new HttpEntity<String>(sb.toString(), headers);
                String url = "http://124.70.28.147:8089/u8cloud/api/gl/voucher/insert";
                String results = restTemplate.postForEntity(url, formEntity, String.class).getBody();
                System.out.println("result:"+results);
                JsonParser jp = new JsonParser();
                //将json字符串转化成json对象
                JsonObject jo = jp.parse(results).getAsJsonObject();
                //获取errcode对应的值
                String status = jo.get("status").getAsString();

                if(status.equals("falied")){
                    String errormsg = jo.get("errormsg").getAsString();
                    BtyHycz hycz=btyHyczService.getOne(new QueryWrapper<BtyHycz>().eq("dm",dm));
                    if(ToolUtil.isNotEmpty(hycz)){
                        hycz.setErrormsg(errormsg);
                        btyHyczService.updateById(hycz);
                    }
                }else{
                    BtyHycz hycz=btyHyczService.getOne(new QueryWrapper<BtyHycz>().eq("dm",dm));
                    if(ToolUtil.isNotEmpty(hycz)){
                        hycz.setFlag(1);
                        btyHyczService.updateById(hycz);
                    }
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
            jsonResult.setState(0);
            jsonResult.setErrmsg(e.getMessage());
            return jsonResult;
        }
        return true;
    }

}


